import Image
from PIL import ImageGrab
from PIL import ImageChops
import time
import os

from colorama import init as coloramainit 
from colorama import Fore, Back, Style

from stich import equNull

coloramainit()


curmapx=0
curmapy=300

lastim=Image.new("RGB",(125,125),0)

def testBlack(c):
    c=c.getdata()
    if testIndoor(c):    
        return True
    if testBad(c):
        return True
    for x in xrange(125):
        for y in xrange(125):
            if c.getpixel((x,y)) == (0, 0 ,0):
                pass
            else:                
                return False
    return True

def testBad(c):
    ##print c.getpixel((0,0))
    ##print c.getpixel((124,124))
    ##print c.getpixel((124,0))
    ##print c.getpixel((0,124))
    if equNull(c.getpixel((0,0))) and \
       equNull(c.getpixel((124,124))) and \
       equNull(c.getpixel((0,124))) and \
       equNull(c.getpixel((124,0))):
        return True
    else:
        return False
def testIndoor(c):
    (r, g ,b)=c.getpixel((116,116))
    return abs(r-211)+abs(g-26)+abs(b-156)<5
        
def testIfNew(a,b):
    c=ImageChops.subtract(a,b)
    
    return not testBlack(c)

def testForMap(im,x,y):
    #return True# debug
    
    if im.getpixel((x,y))==(210,178,114):
        if im.getpixel((x+1,y))==(81,57,7):
            if im.getpixel((x,y+1))==(81,57,7):
                return True
    return False

def updateCurmap():
    global curmapx
    global curmapy
    fullscreen=ImageGrab.grab()
    screenWidth=fullscreen.getbbox()[2]
    screenHeight=fullscreen.getbbox()[3]
    for x in xrange(screenWidth-500):
        for y in xrange(screenHeight-300):
            if testForMap(fullscreen,x,300+y):
                if curmapx!=x+1 or curmapy!=300+y+1:
                    curmapx=x+1
                    curmapy=300+y+1
                    NOMAP=False
                    print '\033[34;1m'+'Map found in:'+'\033[0;22m',curmapx,curmapy
                return True
    return False


def grabMap(path):
    global lastim
    global curmapx
    global curmapy

    if not updateCurmap():
        print '\033[31;1m'+'NOMAP! start HnH'+'\033[0;22m'
    else:
        im=ImageGrab.grab((curmapx,curmapy,curmapx+125,curmapy+125))
        if testIfNew(im,lastim):
            lastim=im
            if not os.path.isdir(path):
                os.mkdir(path)
            im.save(path+str(time.localtime()[0])+'-'+str("%02d" % time.localtime()[1])+'-'+str(time.localtime()[2])+'_'+time.ctime()[11:-5].replace(':','-')+'.bmp')

            print '\033[34;1m'+path+str(time.localtime()[0])+'-'+str("%02d" % time.localtime()[1])+'-'+str(time.localtime()[2])+'_'+time.ctime()[11:-5].replace(':','-')+'.bmp saved'+'\033[0;22m'
        else:
            print '\033[34m'+'standing'+'\033[0m'

def grabMapthread(path):
    while True:
        grabMap(path)
        time.sleep(7)

if __name__=='__main__':
    path=".\\maps\\"
    sleeptime=7
    print '\033[1m'+'[map grabber]'+'\033[22m'
    grabMapthread(path)